home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / geometry / cmodel / cmodel_data.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-09  |  6.4 KB  |  290 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "cmodelP.h"
  4.  
  5. /* created from link_list.c */
  6.  
  7. struct vertex_block {
  8.    struct vertex_block *next;
  9.    struct vertex block[ vertexBLOCKSIZE ];
  10.    };
  11.  
  12. static struct vertex_block first_vertex_block, *curr_vertex_block;
  13. static struct vertex *last_vertex;
  14.  
  15. static int vx_count = 0;
  16.  
  17. int vertex_count()
  18. {
  19.    return vx_count;
  20.    }
  21.  
  22. /* once off initialization at the start */
  23.  
  24. void initialize_vertexs()
  25. {
  26.    curr_vertex_block = &first_vertex_block;
  27.    first_vertex_block.next = NULL;
  28.    last_vertex = first_vertex_block.block;
  29.    first_vertex_block.block->next = NULL;
  30.    vx_count = 0;
  31.    return;
  32.    }
  33.  
  34. struct vertex *first_vertex()
  35. {
  36.    return first_vertex_block.block->next;
  37.    }
  38.  
  39. void clear_all_vertexs()
  40. {
  41.    struct vertex_block *this_block, *tmp;
  42.    
  43.    /* free each block */
  44.    this_block = first_vertex_block.next;
  45.    while(this_block != NULL) {
  46.       tmp = this_block->next;
  47.       free(this_block);
  48.       this_block = tmp;
  49.       }
  50.    
  51.    initialize_vertexs();
  52.    return;
  53.    }
  54.  
  55. struct vertex_block *new_vertex_block()
  56. {
  57.    struct vertex_block *n_block;
  58.    
  59.    curr_vertex_block->next = n_block = 
  60.       (struct vertex_block *)malloc(sizeof(struct vertex_block));
  61.    n_block->next = NULL;
  62.    return curr_vertex_block = n_block;
  63.    }
  64.  
  65. struct vertex *new_vertex(Point3 *pt, struct vertex *v1, struct vertex *v2)
  66. {
  67.    struct vertex *n_vertex;
  68.    
  69.    /* allocate a new vertex and link it in */
  70.    n_vertex = last_vertex + 1;
  71.    if (n_vertex - curr_vertex_block->block >= vertexBLOCKSIZE)
  72.       n_vertex = (new_vertex_block())->block;
  73.  
  74.    last_vertex->next = n_vertex;
  75.    n_vertex->next = NULL;
  76.    last_vertex = n_vertex;
  77.    vx_count++;
  78.    
  79.    /* now initialize n_vertex */
  80.    Pt3Copy(pt, (Point3 *)&n_vertex->V.pt);
  81.    n_vertex->V.pt.w = 1.;
  82.    /* interpolate between two vertex colors */
  83.    if (v2 != NULL) {
  84.       n_vertex->V.vcol.r = .5 * v1->V.vcol.r + .5 * v2->V.vcol.r;
  85.       n_vertex->V.vcol.g = .5 * v1->V.vcol.g + .5 * v2->V.vcol.g;
  86.       n_vertex->V.vcol.b = .5 * v1->V.vcol.b + .5 * v2->V.vcol.b;
  87.       n_vertex->V.vcol.a = .5 * v1->V.vcol.a + .5 * v2->V.vcol.a;
  88.       } 
  89.    else
  90.       CoACopy(&v1->V.vcol, &n_vertex->V.vcol);
  91.    n_vertex->visible = FALSE;
  92.    HPt3Copy(&v1->polar, &n_vertex->polar);
  93.    
  94.    return n_vertex;
  95.    }
  96.  
  97. struct vertex *simple_new_vertex(HPoint3 *pt, ColorA *col)
  98. {
  99.    struct vertex *n_vertex;
  100.    
  101.    /* allocate a new vertex and link it in */
  102.    n_vertex = last_vertex + 1;
  103.    if (n_vertex - curr_vertex_block->block >= vertexBLOCKSIZE)
  104.       n_vertex = (new_vertex_block())->block;
  105.  
  106.    last_vertex->next = n_vertex;
  107.    n_vertex->next = NULL;
  108.    last_vertex = n_vertex;
  109.    vx_count++;
  110.    
  111.    /* now initialize n_vertex */
  112.    HPt3Copy(pt, &n_vertex->V.pt);
  113.    CoACopy(col, &n_vertex->V.vcol);
  114.    n_vertex->visible = FALSE;
  115.    /* polar is not used in this case */
  116.       
  117.    return n_vertex;
  118.    }
  119.  
  120. struct edge_block {
  121.    struct edge_block *next;
  122.    struct edge block[ edgeBLOCKSIZE ];
  123.    };
  124.  
  125. static struct edge_block first_edge_block, *curr_edge_block;
  126. static struct edge *last_edge;
  127.  
  128. /* once off initialization at the start */
  129.  
  130. void initialize_edges()
  131. {
  132.    curr_edge_block = &first_edge_block;
  133.    first_edge_block.next = NULL;
  134.    last_edge = first_edge_block.block;
  135.    first_edge_block.block->next = NULL;
  136.    return;
  137.    }
  138.  
  139. struct edge *first_edge()
  140. {
  141.    return first_edge_block.block->next;
  142.    }
  143.  
  144. struct edge *get_last_edge()
  145. {
  146.    return last_edge;
  147.    }
  148.  
  149. void clear_all_edges()
  150. {
  151.    struct edge_block *this_block, *tmp;
  152.    
  153.    /* free each block */
  154.    this_block = first_edge_block.next;
  155.    while(this_block != NULL) {
  156.       tmp = this_block->next;
  157.       free(this_block);
  158.       this_block = tmp;
  159.       }
  160.    
  161.    initialize_edges();
  162.    return;
  163.    }
  164.  
  165. struct edge_block *new_edge_block()
  166. {
  167.    struct edge_block *n_block;
  168.    curr_edge_block->next = n_block = 
  169.       (struct edge_block *)malloc(sizeof(struct edge_block));
  170.    n_block->next = NULL;
  171.    return curr_edge_block = n_block;
  172.    }
  173.  
  174. struct edge *new_edge(struct vertex *v1, struct vertex *v2, HPoint3 *polar)              
  175. {
  176.    struct edge *n_edge;
  177.    
  178.    /* allocate a new edge and link it in */
  179.    n_edge = last_edge + 1;
  180.    if (n_edge - curr_edge_block->block >= edgeBLOCKSIZE)
  181.       n_edge = (new_edge_block())->block;
  182.  
  183.    last_edge->next = n_edge;
  184.    n_edge->next = NULL;
  185.    last_edge = n_edge;
  186.    
  187.    /* now initialize n_edge */
  188.    n_edge->v1 = v1;
  189.    n_edge->v2 = v2;
  190.    HPt3Copy(polar, &n_edge->polar);
  191.    n_edge->small = n_edge->split = n_edge->visible = n_edge->hascolor = FALSE;
  192.    n_edge->other_half = NULL;
  193.    
  194.    return n_edge;
  195.    }
  196.  
  197. /* created from link_list.c */
  198.  
  199. struct triangle_block {
  200.    struct triangle_block *next;
  201.    struct triangle block[ triangleBLOCKSIZE ];
  202.    };
  203.  
  204. static struct triangle_block first_triangle_block, *curr_triangle_block;
  205. static struct triangle *last_triangle;
  206.  
  207. static int tri_count = 0;
  208.  
  209. int triangle_count()
  210. {
  211.    return tri_count;
  212.    }
  213.  
  214. /* once off initialization at the start */
  215.  
  216. void initialize_triangles()
  217. {
  218.    curr_triangle_block = &first_triangle_block;
  219.    first_triangle_block.next = NULL;
  220.    last_triangle = first_triangle_block.block;
  221.    first_triangle_block.block->next = NULL;
  222.    tri_count = 0;
  223.    return;
  224.    }
  225.  
  226. struct triangle *first_triangle()
  227. {
  228.    return first_triangle_block.block->next;
  229.    }
  230.  
  231. struct triangle *get_last_triangle()
  232. {
  233.    return last_triangle;
  234.    }
  235.  
  236. void clear_all_triangles()
  237. {
  238.    struct triangle_block *this_block, *tmp;
  239.    
  240.    /* free each block */
  241.    this_block = first_triangle_block.next;
  242.    while(this_block != NULL) {
  243.       tmp = this_block->next;
  244.       free(this_block);
  245.       this_block = tmp;
  246.       }
  247.    
  248.    initialize_triangles();
  249.    return;
  250.    }
  251.  
  252. struct triangle_block *new_triangle_block()
  253. {
  254.    struct triangle_block *n_block;
  255.    
  256.    curr_triangle_block->next = n_block = 
  257.       (struct triangle_block *)malloc(sizeof(struct triangle_block));
  258.    n_block->next = NULL;
  259.    return curr_triangle_block = n_block;
  260.    }
  261.  
  262. struct triangle *new_triangle(struct edge *e1, struct edge *e2, 
  263.    struct edge *e3, int o1, int o2, int o3, Poly *orig)
  264. {
  265.    struct triangle *t;
  266.    
  267.    /* allocate a new triangle and link it in */
  268.    t = last_triangle + 1;
  269.    if (t - curr_triangle_block->block >= triangleBLOCKSIZE)
  270.       t = (new_triangle_block())->block;
  271.  
  272.    last_triangle->next = t;
  273.    t->next = NULL;
  274.    last_triangle = t;
  275.    
  276.    /* now initialize t */
  277.    t->small = FALSE;
  278.    t->e1 = e1;
  279.    t->e2 = e2;
  280.    t->e3 = e3;
  281.    t->o1 = o1;
  282.    t->o2 = o2;
  283.    t->o3 = o3;
  284.    t->orig_poly = orig;
  285.    ++tri_count;
  286.    
  287.    return t;
  288.    }
  289.  
  290.